package Classes.GameViews.ViewPanels// ActionScript file
{
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GameData;
	import Classes.GameErrors.GameError;
	import Classes.GameInformations.GameInformation;
	import Classes.GameUtils.GU_Sound;
	import Classes.GameViews.GV_Map;
	import Classes.GameViews.GameView;
	import Classes.GameViews.ViewScenes.ViewScene;
	import Classes.Interfaces.I_Observer;
	
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getQualifiedClassName;
	
	/**
	 *游戏界面面板 
	 * @author 葛增文
	 * 
	 */	
	public class ViewPanel extends GameView implements I_Observer
	{
		private var _showScene:ViewScene;/*显示场景*/
		private var _observeFuncs:Object;/*观察方法存储器*/
		private var _updateTypes:Object;/*更新需要实现的方法*/
		private var _updateSign:Boolean;/*更新标记*/
		private var _isCenter:Boolean=false;/*是否要居中*/
		protected var _backObject:Object={x:0,y:0,scaleX:0.1,scaleY:0.1};
		
		static public var openNumber:int	=	0;			//打开的面板数
		public function ViewPanel(lockedScreen:Boolean=false,dragEnabled:Boolean=false):void
		{
			this.init();
			this.cacheAsBitmap=true;
		}
		
		private function init():void
		{
			this._observeFuncs=new Object;
			this._updateTypes=new Object;

			this.addObserveFunc(GD_Player.instance,"fullScreen",fullScreenHD,false);
		}
		
		/**
		 *更新 
		 * 
		 */		
		protected function update():void
		{
			for(var updateType:String in this.updateTypes)
			{
				if(!this.updateTypes.hasOwnProperty(updateType))throw new GameError("该方法尚未注册");
				var updateFunc:Function=this.observeFuncs[updateType];
				updateFunc();
			}
			this._updateSign=false;
			this._updateTypes=new Object;
		}
		
		/**
		 *增加观察方法 
		 * @param observeTarget
		 * @param observeType
		 * @param func
		 * 
		 */		
		protected function addObserveFunc(observeTarget:GameData,observeType:String,func:Function,isTouchInFirst:Boolean=true):void
		{
			observeTarget.addObserver(observeType,this);
			this.observeFuncs[observeType]=func;
			if(isTouchInFirst)
			{
				this.touchObserve(observeType);
			}
		}
		
		/**
		 *在指定场景打开界面 
		 * @param vs
		 * 
		 */	
		private var bgSp:Sprite=null;
		private var haveBg:Boolean;
		/**
		 * 打开浮动窗口 
		 * @param vs
		 * @param bg   是否加入黑色背景
		 * @param sd   是否播放声效
		 */		
		public function openAt(vs:ViewScene,bg:Boolean=false,sd:Boolean=true):void
		{
//			GV_Map.instance.buildMode	=	GV_Map.MOVEMODE;
			haveBg	=	bg;
			if( this.isFJZpanel()){
				openNumber+=1;
				if(openNumber>0)GV_Map.instance.buildMode	=	GV_Map.MOVEMODE;
			}
			
			this._showScene=vs;
			if(this.showScene)
			{
				if(sd)this.openSound();
				
				if(bg && (null==bgSp || !this.showScene.layerPanel.contains(bgSp)))
				{
					bgSp=new Sprite();
					bgSp.graphics.beginFill(0x000000,0.5);
					bgSp.graphics.drawRect(0,0,ZombieTown.instance.sysStage.stageWidth,ZombieTown.instance.sysStage.stageHeight);
					bgSp.graphics.endFill();
					var bgObj:DisplayObject=this.showScene.layerPanel.addChild(bgSp);
				}
				this.showScene.layerPanel.addChild(this);
//				if(this.showScene.layerPanel.contains((VP_OpenWinView.instance) as DisplayObject))
//				{
//					var childLen:Number=this.showScene.layerPanel.numChildren;
//					this.showScene.layerPanel.addChild((VP_OpenWinView.instance) as DisplayObject);
//				}
				
				if(this.updateSign)
				{
					this.update();
					this.updateSign=false;
				}
			}
		}
		
		
		protected function fullScreenHD():void
		{
			if(_isCenter)
			{
				toCenter();
//				VP_Main.instance.isHideMainPanel=false;
			}
			
			
		}
		
		/**
		 *置中 
		 * 
		 */		
		protected function toCenter():void
		{
//			this.x=(ZombieTown.instance.sysStage.stageWidth-this.width)/2;
//			this.y=(ZombieTown.instance.sysStage.stageHeight-this.height)/2;
			//this.x=this.y=0;

			this._isCenter	=	true;
			this.scaleX		=	this.scaleY		=	1;
			var cx:Number	=	(ZombieTown.instance.sysStage.stageWidth-this.width)/2;
			var cy:Number	=	(ZombieTown.instance.sysStage.stageHeight-this.height)/2;
			this.scaleX=this._backObject.scaleX;
			this.scaleY=this._backObject.scaleY;

			
			if(this is VP_message){
				this.x	=	this.backPositionPoint.x;
				this.y	=	this.backPositionPoint.y;
			}
			
			TweenLite.to(this,0.5,{scaleX:1,scaleY:1,x:cx,y:cy});
		}
		
		/**
		 *触发观察 
		 * @param observeType
		 * 
		 */		
		public function touchObserve(observeType:String):void
		{
			this.updateTypes[observeType]=true;
			if(this.stage && !this.updateSign)
			{
				stage.addEventListener(Event.RENDER,updateHandler,false,0,true);
				stage.invalidate();
			}
			this._updateSign=true;
		}
		
		private function updateHandler(e:Event):void
		{
			this.update();
		}
		
		/**
		 *关闭界面 
		 * 
		 */		
		public function close(sound:Boolean=true):void
		{
			if( this.isFJZpanel()){
				openNumber-=1;
				if(openNumber<0)openNumber=0;
				if(openNumber<1){
					  if(!VP_Main.instance.interfaceUI["isBuildMode"])
						GV_Map.instance.buildMode	=	GV_Map.CANNOTBUILDMODE;
				}
			}
			
			
			if(this is VP_errorPanel){
				if(this.parent)
				{
					if(sound)closeSound();
					this.parent.removeChild(this);
					while( bgSp && this.showScene.layerPanel.contains(bgSp))
					{
						this.showScene.layerPanel.removeChild(bgSp);
						bgSp.graphics.clear();
						bgSp=null;
					}
				}
				return;
			}
//			var cx:Number	=	(ZombieTown.instance.sysStage.stageWidth-this.width)/2;
//			var cy:Number	=	(ZombieTown.instance.sysStage.stageHeight-this.height)/2;
//			this.scaleX=0.1;
//			this.scaleY=0.1;
			if(this.parent)
			{
				if(sound)closeSound();
			}
			if(null==this.stage)
			{
				this.closehd();
			}else{
				if(this.backPositionPoint && (this.backPositionPoint.x!=0 || this.backPositionPoint.y!=0) )
					TweenLite.to(this,0.5,{scaleX:this._backObject.scaleX,scaleY:this._backObject.scaleY,x:this.backPositionPoint.x,y:this.backPositionPoint.y,onComplete:closehd});
				else
				TweenLite.to(this,0.5,{scaleX:this._backObject.scaleX,scaleY:this._backObject.scaleY,x:stage.stageWidth*0.1,y:stage.stageHeight*0.1,onComplete:closehd});
			}
		}
		
		/**
		 *关闭处理 
		 * 
		 */
		public function closehd():void
		{
			if(this.parent)
			{
				this.parent.removeChild(this);
				this.scaleX		=	this.scaleY		=	1;
				while( bgSp && this.showScene.layerPanel.contains(bgSp))
				{
					this.showScene.layerPanel.removeChild(bgSp);
				}
			}
		}
		
		private function closeSound():void
		{
			var string:String	=	getQualifiedClassName(this);
			string=string.replace("::",".");//替换字符 :: 为 .
			string=string.substr(string.lastIndexOf(".")+1);//提取 . 后边的字符 就是类名
			switch(string){
				case	"VP_TextWinPanel":
				case 	"VP_beathartWindon":
				case 	"VP_BubbleUp":
				case 	"VP_exChangePanel":
				case 	"VP_FastBuy_actPoint":
				case 	"VP_FastBuy_weapon":	
				case 	"VP_Mission":
				case   "VP_SevenDaysRewardUI":	
				case 	"VP_QQhzDayGift":	
				case	"VP_message":
					GU_Sound.instance.playCommSound("Button_Close");
					break;
				case 	"VP_StorePanel":
				case 	"VP_Make":
				case 	"VP_Building":	
				case 	"VP_Package":	
					GU_Sound.instance.playCommSound("Button_Building_Close");
					break;
				case 	"VP_NewQQVip":
				case 	"VP_NPCDialog":
				case 	"VP_NPCFlashPlayView":
				case 	"VP_OpenWinView":
				case 	"VP_QQTQView":
				case    "VP_RewardItemWindon":
				case 	"VP_selectSex":
				case 	"VP_errorPanel":
				default:
			}
		}
		
		private function openSound():void
		{
			var string:String	=	getQualifiedClassName(this);
			string=string.replace("::",".");//替换字符 :: 为 .
			string=string.substr(string.lastIndexOf(".")+1);//提取 . 后边的字符 就是类名
			switch(string){
				case 	"VP_beathartWindon":
				case 	"VP_BubbleUp":
				case 	"VP_FastBuy_actPoint":
				case 	"VP_FastBuy_weapon":	
					GU_Sound.instance.playCommSound("System_Prompt");
					break;
				case 	"VP_StorePanel":
				case 	"VP_Make":
				case 	"VP_Building":	
				case 	"VP_Package":	
					GU_Sound.instance.playCommSound("Button_Building_Open");
					break;
				case 	"VP_Mission":	
				case 	"VP_exChangePanel":
				case 	"VP_NewQQVip":
				case 	"VP_NPCDialog":
				case 	"VP_NPCFlashPlayView":
				case	"VP_TextWinPanel":	
				case 	"VP_OpenWinView":
				case 	"VP_SevenDaysRewardUI":
				case 	"VP_QQhzDayGift":	
				case	"VP_message":	
					GU_Sound.instance.playCommSound("Button_Pressed");
					break;
				case 	"VP_QQTQView":
				case    "VP_RewardItemWindon":
				case 	"VP_selectSex":
				case 	"VP_errorPanel":	
				default:
			}
		}
		
		
		/**
		 *是否是弹出来然后其它地图对像不能动 
		 * @return 
		 * 
		 */
		public function isFJZpanel():Boolean
		{
			if( this.haveBg)return true;
			else return false;
			
			var string:String	=	getQualifiedClassName(this);
			string=string.replace("::",".");//替换字符 :: 为 .
			string=string.substr(string.lastIndexOf(".")+1);//提取 . 后边的字符 就是类名
			switch(string){
				case 	"VP_beathartWindon":
				case 	"VP_BubbleUp":
				case 	"VP_FastBuy_actPoint":
				case 	"VP_FastBuy_weapon":	
				case 	"VP_StorePanel":
				case 	"VP_Make":
				case 	"VP_Building":	
				case 	"VP_Package":	
				case 	"VP_Mission":	
				case 	"VP_exChangePanel":
				case 	"VP_NewQQVip":
				case 	"VP_NPCDialog":
				case 	"VP_NPCFlashPlayView":
				case	"VP_TextWinPanel":	
				case 	"VP_OpenWinView":
				case 	"VP_SevenDaysRewardUI":
				case 	"VP_QQhzDayGift":	
				case	"VP_message":	
				case 	"VP_QQTQView":
				case    "VP_RewardItemWindon":
				case 	"VP_selectSex":
				case   "VP_DayRewardUI":
				case   "VP_NewWeaponWin":
//				case 	"VP_errorPanel":	
				case 	"PorgramNpcDialogue":
					return true;
				default:return false;
			}
		}
		
		
		public function cleanAllObserves():void
		{
			
		}
		
		public function release():void
		{
			this.cleanAllObserves();
			if(this.parent)this.parent.removeChild(this);
			while(this.numChildren)this.removeChildAt(0);
		}

		/*getter & setter*/
		public function get showScene():ViewScene
		{
			return _showScene;
		}

		public function get observeFuncs():Object
		{
			return _observeFuncs;
		}

		public function get updateTypes():Object
		{
			return _updateTypes;
		}

		public function get updateSign():Boolean
		{
			return _updateSign;
		}

		public function set updateSign(value:Boolean):void
		{
			_updateSign = value;
		}
		
		/**
		 *设置反回到的对像 
		 * @param value
		 * 
		 */
		public function set backObject(value:Object):void
		{
			this._backObject	=	value;
			if(this._backObject==null)return;
			this.x	=	this.backPositionPoint.x;
			this.y	=	this.backPositionPoint.y;
		}
		
		/**
		 * 全屏与突出全屏的位置矫正 
		 * 
		 */		
		public function updatePosition():void
		{
			this.x=(ZombieTown.instance.sysStage.stageWidth-this.width)/2;
			this.y=(ZombieTown.instance.sysStage.stageHeight-this.height)/2;
		}
		
		/**
		 *返回面板起始位置 
		 * 
		 */
		public function get backPositionPoint():Point
		{
			if(this._backObject==null)return null;
			return new Point(this._backObject.x,this._backObject.y);//this._backObject.localToGlobal(new Point(0,0));
		}
		
		public function set isCenter(v:Boolean):void
		{
			this._isCenter	=	v;
		}
	}
}